<?php
defined('BASEPATH') or exit('No direct script access allowed');

class BaseDataCollector extends CI_Controller
{

	public function __construct()
	{
		parent::__construct();
		$this->load->model('Futures_history_model'); // 假设有一个名为Example_model的模型
		$this->load->model('Sync_date_model');

		$this->load->library('stringutil');
	}

	public function collect()
	{
		set_time_limit(0);

		// 起始日期
		$date = new DateTime('2012-01-01 00:00:00');
		$db_date = $this->Sync_date_model->maxDate('czce');
		if ($db_date) {
			$date = new DateTime("$db_date 00:00:00");
		}

		$today_date = date('Y-m-d');

		while ($date->format('Y-m-d') < $today_date) {

			log_message("error", "collect, date: " . $date->format('Y-m-d'));

			// 使脚本休眠 100000 微秒（即 0.1 秒）
			usleep(100000);

			$date_string = $date->format('Y-m-d');
			// 判断记录表中有没有日期
			$sync_date = $this->Sync_date_model->selectByDate($date_string, 'czce');
			if ($sync_date) {
				$date->modify('+1 day');
				continue;
			}

			/*************************处理郑商所数据********************** */
			$year = $date->format('Y');
			$date_string2 = $date->format('Ymd');

			if ($date_string <= "2015-11-11") {
				$result = $this->saveData1($year, $date_string2);
				log_message("error", "collect, saveData1, result: " . json_encode($result));
			} else {
				$result = $this->saveData2($year, $date_string2);
				log_message("error", "collect, saveData2, result: " . json_encode($result));
			}

			$date->modify('+1 day');
		}
	}
	public function saveData1($year, $date)
	{
		$date_time = date('Y-m-d H:i:s');

		// 1、从 url 中获取数据
		// http://www.czce.com.cn/cn/exchange/2012/datadaily/20120104.htm
		$url = "http://www.czce.com.cn/cn/exchange/$year/datadaily/$date.txt";
		$contents = @file_get_contents($url);

		if ($contents === FALSE) {
			$sync_date = array("platform" => "czce", "date" => $date, "status" => "N", "created_at" => $date_time);
			$this->Sync_date_model->insert($sync_date);
			log_message("error", "saveData1, 没有获取到数据, year: $year, date, $date");
			return true;
		}

		// 替换回车
		$normalizedString = str_replace(array("\r\n", "\r"), "\n", $contents);

		// 2、分析数据
		$insert_data_list = array();
		// 将数据分割为数组
		$data_list = explode("\n", $normalizedString);
		foreach ($data_list as $data) {

			// 2-1、数据中没有逗号，continue
			$position = strpos($data, ',');
			if (!$position) {
				continue;
			}

			// 2-2、以逗号为分隔符，分割数据
			$row = explode(",", $data);

			// 2-3、判断是否为 “小计”
			if (count($row) > 1 && $row[1] == '') {
				continue;
			}

			// 2-4、整理数据
			$id = $this->stringutil->generateUUID();
			$insert_data = array(
				"id" => $id,
				"platform" => "czce",
				"date" => $date,
				"symbol" => trim($row[0]),
				"last_settlement" => $row[1],
				"open" => $row[2],
				"high" => $row[3],
				"low" => $row[4],
				"close" => $row[5],
				"settlement" => $row[6],
				"rf1" => $row[7],
				"rf2" => $row[8],
				"volume" => $row[9],
				"open_interest" => $row[10],
				"id_volume" => $row[11],
				"t_volume" => $row[12],
				"d_price" => $row[13],
				"created_at" => $date_time,
				"updated_at" => $date_time
			);

			array_push($insert_data_list, $insert_data);
		}

		// 3、历史数据入库
		if ($insert_data_list) {
			$insert_result = $this->Futures_history_model->insert_batch($insert_data_list);
			log_message("error", "saveData1, 入库结果: " . json_encode($insert_result) . ", year: $year, date, $date");
		}

		// 4、入库后，记录时间
		$sync_date = array("platform" => "czce", "date" => $date, "status" => "Y", "created_at" => $date_time);
		$this->Sync_date_model->insert($sync_date);

		log_message("error", "saveData1, success.");
		return true;
	}

	public function saveData2($year, $date)
	{
		$date_time = date('Y-m-d H:i:s');
		// $error_code = ["合约代码", "品种月份", "小计", "总计"];

		// 1、从 url 中获取数据
		// http://www.czce.com.cn/cn/DFSStaticFiles/Future/2015/20151112/FutureDataDaily.htm
		$url = "http://www.czce.com.cn/cn/DFSStaticFiles/Future/$year/$date/FutureDataDaily.txt";
		$contents = @file_get_contents($url);

		if ($contents === FALSE) {
			$sync_date = array("platform" => "czce", "date" => $date, "status" => "N", "created_at" => $date_time);
			$this->Sync_date_model->insert($sync_date);
			log_message("error", "saveData2, 没有获取到数据, year: $year, date, $date");
			return true;
		}

		// 替换回车
		$normalizedString = str_replace(array("\r\n", "\r"), "\n", $contents);

		// 2、分析数据
		$insert_data_list = array();
		// 将数据分割为数组
		$data_list = explode("\n", $normalizedString);
		foreach ($data_list as $data) {

			// 2-1、数据中没有竖线，continue
			$position = strpos($data, '|');
			if (!$position) {
				continue;
			}

			// 2-2、以竖线为分隔符，分割数据
			$row = explode("|", $data);

			// 2-3、判断是否为 小计 等信息
			if (count($row) > 1 && !preg_match('/\d/', $row[1])) {
				continue;
			}

			// 2-4、整理数据
			$id = $this->stringutil->generateUUID();
			$insert_data = array(
				"id" => $id,
				"platform" => "czce",
				"date" => $date,
				"symbol" => trim($row[0]),
				"last_settlement" => $row[1] != '' ? str_replace(",", "", $row[1]) : 0,
				"open" => $row[2] != '' ? str_replace(",", "", $row[2]) : 0,
				"high" => $row[3] != '' ? str_replace(",", "", $row[3]) : 0,
				"low" => $row[4] != '' ? str_replace(",", "", $row[4]) : 0,
				"close" => $row[5] != '' ? str_replace(",", "", $row[5]) : 0,
				"settlement" => $row[6] != '' ? str_replace(",", "", $row[6]) : 0,
				"rf1" => $row[7] != '' ? str_replace(",", "", $row[7]) : 0,
				"rf2" => $row[8] != '' ? str_replace(",", "", $row[8]) : 0,
				"volume" => $row[9] != '' ? str_replace(",", "", $row[9]) : 0,
				"open_interest" => $row[10] != '' ? str_replace(",", "", $row[10]) : 0,
				"id_volume" => $row[11] != '' ? str_replace(",", "", $row[11]) : 0,
				"t_volume" => $row[12] != '' ? str_replace(",", "", $row[12]) : 0,
				"d_price" => $row[13] != '' ? str_replace(",", "", $row[13]) : 0,
				"created_at" => $date_time,
				"updated_at" => $date_time
			);

			array_push($insert_data_list, $insert_data);
		}

		// 3、历史数据入库
		if ($insert_data_list) {
			$insert_result = $this->Futures_history_model->insert_batch($insert_data_list);
			log_message("error", "saveData2, 入库结果: " . json_encode($insert_result) . ", year: $year, date, $date");
		}

		// 4、入库后，记录时间
		$sync_date = array("platform" => "czce", "date" => $date, "status" => "Y", "created_at" => $date_time);
		$this->Sync_date_model->insert($sync_date);

		log_message("error", "saveData2, success.");
		return true;
	}

	public function collect2()
	{
		set_time_limit(0);

		// 起始日期
		$date = new DateTime('2012-01-01 00:00:00');
		$db_date = $this->Sync_date_model->maxDate('dce');
		if ($db_date) {
			$date = new DateTime("$db_date 00:00:00");
		}

		$today_date = date('Y-m-d');

		while ($date->format('Y-m-d') < $today_date) {

			log_message("error", "collect2, date: " . $date->format('Y-m-d'));

			// 使脚本休眠 100000 微秒（即 0.1 秒）
			usleep(100000);

			$date_string = $date->format('Y-m-d');
			// 判断记录表中有没有日期
			$sync_date = $this->Sync_date_model->selectByDate($date_string, 'dce');
			if ($sync_date) {
				$date->modify('+1 day');
				continue;
			}

			$year = $date->format('Y');
			$month = $date->format('m');
			$day = $date->format('d');

			/*************************处理大商所数据********************** */
			$result = $this->saveData3($year, $month, $day);
			log_message("error", "collect2, saveData3, result: " . json_encode($result));

			$date->modify('+1 day');
		}
	}

	public function saveData3($year, $month, $day)
	{
		$date = date('Y-m-d', strtotime($year .'-'. $month .'-'. $day));
		$now_time = date('Y-m-d H:i:s');

		// 1、请求 url 参数
		$url = "http://www.dce.com.cn/publicweb/quotesdata/exportDayQuotesChData.html";
		$param = [
			'dayQuotes.variety' => 'all',
			'dayQuotes.trade_type' => '0',
			'year' => $year,
			'month' => (int)$month - 1,
			'day' => $day,
			'exportFlag' => 'txt'
		];

		// 将数据转换为URL编码的查询字符串
		$postData = http_build_query($param);

		// 创建HTTP上下文选项
		$options = [
			'http' => [
				'header' => "Content-type: application/x-www-form-urlencoded\r\n",
				'method' => 'POST',
				'content' => $postData,
			],
		];

		// 创建上下文流
		$context = @stream_context_create($options);

		// 使用file_get_contents发送请求并获取响应
		$contents = @file_get_contents($url, false, $context);

		if ($contents === FALSE) {
			$sync_date = array("platform" => "dce", "date" => $date, "status" => "N", "created_at" => $now_time);
			$this->Sync_date_model->insert($sync_date);
			log_message("error", "saveData3, 没有获取到数据, year: $year, month: $month, day: $day");
			return true;
		}

		// 替换 两个 tab 键
		$normalizedString1 = str_replace('		', '|', $contents);
		$normalizedString2 = str_replace('	', '|', $normalizedString1);

		// 替换回车
		$normalizedString = str_replace(array("\r\n", "\r"), "\n", $normalizedString2);

		// 2、分析数据
		$insert_data_list = array();
		// 将数据分割为数组
		$data_list = explode("\n", $normalizedString);

		foreach ($data_list as $data) {

			// 2-1、以 | 为分隔符，分割数据
			$row = explode("|", $data);

			// 2-2、如果 row 的数量小于2，或者第二个值没有数字，则 continue
			if (count($row) < 2 || ! preg_match('/\d/', $row[1])) {
				continue;
			}
			if (count($row) < 14) {
				continue;
			}

			// 2-3、整理数据
			$id = $this->stringutil->generateUUID();
			$insert_data = array(
				"id" => $id,
				"platform" => "dce",
				"date" => "$year$month$day",
				"symbol" => trim($row[1]),
				"last_settlement" => $row[6] != '' ? str_replace(",", "", $row[6]) : 0,
				"open" => $row[2] != '' ? str_replace(",", "", $row[2]) : 0,
				"high" => $row[3] != '' ? str_replace(",", "", $row[3]) : 0,
				"low" => $row[4] != '' ? str_replace(",", "", $row[4]) : 0,
				"close" => $row[5] != '' ? str_replace(",", "", $row[5]) : 0,
				"settlement" => $row[7] != '' ? str_replace(",", "", $row[7]) : 0,
				"rf1" => $row[9] != '' ? str_replace(",", "", $row[9]) : 0,
				"rf2" => $row[8] != '' ? str_replace(",", "", $row[8]) : 0,
				"volume" => $row[10] != '' ? str_replace(",", "", $row[10]) : 0,
				"open_interest" => $row[11] != '' ? str_replace(",", "", $row[11]) : 0,
				"id_volume" => $row[12] != '' ? str_replace(",", "", $row[12]) : 0,
				"t_volume" => $row[13] != '' ? str_replace(",", "", $row[13]) : 0,
				"d_price" => null,
				"created_at" => $now_time,
				"updated_at" => $now_time
			);

			array_push($insert_data_list, $insert_data);
		}

		// 3、历史数据入库
		if ($insert_data_list) {
			$insert_result = $this->Futures_history_model->insert_batch($insert_data_list);
			log_message("error", "saveData3, 入库结果: " . json_encode($insert_result) . ", year: $year, date, $date");
		}

		// 4、入库后，记录时间
		$status = $insert_data_list ? "Y" : "N";
		$sync_date = array("platform" => "dce", "date" => $date, "status" => $status, "created_at" => $now_time);
		$this->Sync_date_model->insert($sync_date);

		log_message("error", "saveData3, success.");
		return true;
	}
}
